<?php
namespace Admin\Controller;

use Think\Controller;

class LoginController extends CommonController
{

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 登录界面
     * @return [type]
     */
    public function index()
    {
        $crypt = new \Think\Crypt();
        $user_pwd   = $crypt->decrypt(cookie('logining_admin'), C('ENCRYPT_KEY'));

        if ($user_pwd) {

        	$data = explode('/', $user_pwd);
        	$username = $data[0];
        	$password = $data[1];

        	$result = D('Admin', 'Service')->login($username, $password);

        	if ($result) {
        		
        		redirect(U('index/index'));
        	}
        }

        $this->display();
    }

    /**
     * 登录
     * @return [type] [description]
     */
    public function login()
    {
        $username = I('post.username', '', 'trim');
        $password = I('post.password', '', 'trim');
        $online   = I('post.online', 1, 'intval');

        $result = D('Admin', 'Service')->login($username, $password, $online);

        if (!$result) {
            $this->error('帐号不存在或密码错误', U('login/index'));
        }

        redirect(U('index/index'));
    }

    /**
     * 退出登录
     * @param  string $value [description]
     * @return [type]        [description]
     */
    public function logout()
    {
        session(null);
        cookie('logining_admin', null);

        redirect(U('login/index'));
    }
}
